package com.yihu.cms.service;

import static org.junit.Assert.assertFalse;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.fileManager.DirectoryTreeNode;
import com.yihu.beans.fileManager.Files;
import com.yihu.beans.fileManager.FmListConditions;
import com.yihu.beans.fileManager.Folder;
import com.yihu.beans.user.User;
import com.yihu.dataService.common.DateUtil;
import com.yihu.dataService.hessian.mongodb.interfaceService.FileManagerHService;
import com.yihu.dataService.hessian.resource.ResourceMongoDBManager;

public class FileManagerService {
	
	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	public List<Files> getList(FmListConditions fmlc) {
		List<Files> list = null;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			list = fileManager.getFilesList( fmlc );
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return list;
	}
	
	public List<DirectoryTreeNode> getAllDirectory(){
		List<DirectoryTreeNode> listd = null;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			listd = fileManager.getAllDirectory();
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return listd;
	}

	public int del(Files[] files) { //String path, 
		int result = 0;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			result = fileManager.delFile(files);//path, 
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			e1.printStackTrace();
		}
		return result;
	}

	public int create(Files file) {
		int result = 0;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			result = fileManager.createFile(file);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			log.error("create file error . error=", e1.toString());
		}
		return result;
	}

	/**
	 * 
	 * @param path              "/a/d/3.jpg"
	 * @param newFileName		"4.jpg"
	 * @return 0 normal
	 */
	public int rename(Folder folder) {
		int result = 0;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			result = fileManager.renameFile(folder);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			e1.printStackTrace();
		}
		return result;
	}

	public int move(Files[] files, String newPath) {
		int result = 0;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			result = fileManager.moveFile(files, newPath);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			e1.printStackTrace();
		}
		return result;
	}

	public int copy(Files[] files, String newPath) {
		int result = 0;
		try {
			FileManagerHService fileManager = (FileManagerHService) ResourceMongoDBManager
					.getSingleton().getBeans("fileManagerHSC");
			result = fileManager.copyFile(files, newPath);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			e1.printStackTrace();
		}
		return result;
	}

	public static void main(String args[]) {
		FileManagerService fs = new FileManagerService();

		int result = 0 ;
		
		User user = new User();
		user.setUserName("zhang");
		
		Files afolder = new Files();
		afolder.setFileName("a");
		afolder.setParentFolder("/");
		afolder.setFileType("folder");
		afolder.setDirectory(true);
		afolder.setCreateTime(new Date());
		afolder.setUser(user);
		int afolderResult = fs.create(afolder);
		System.out.println("result afolderResult=" + afolderResult);
		
		Files bfolder = new Files();
		bfolder.setFileName("b");
		bfolder.setParentFolder("/");
		bfolder.setFileType("folder");
		bfolder.setDirectory(true);
		bfolder.setCreateTime(new Date());
		bfolder.setUser(user);
		int bfolderResult = fs.create(bfolder);
		System.out.println("result afolderResult=" + bfolderResult);
		
		Files a1folder = new Files();
		a1folder.setFileName("a1");
		a1folder.setParentFolder("/a");
		a1folder.setFileType("folder");
		a1folder.setDirectory(true);
		a1folder.setCreateTime(new Date());
		a1folder.setUser(user);
		int a1folderResult = fs.create(a1folder);
		System.out.println("result afolderResult=" + a1folderResult);
		
		Files a2folder = new Files();
		a2folder.setFileName("a2");
		a2folder.setParentFolder("/a");
		a2folder.setFileType("folder");
		a2folder.setDirectory(true);
		a2folder.setCreateTime(new Date());
		a2folder.setUser(user);
		int a2folderResult = fs.create(a2folder);
		System.out.println("result afolderResult=" + a2folderResult);
		
		Files a11folder = new Files();
		a11folder.setFileName("a11");
		a11folder.setParentFolder("/a/a1");
		a11folder.setFileType("folder");
		a11folder.setDirectory(true);
		a11folder.setCreateTime(new Date());
		a11folder.setUser(user);
		int a11folderResult = fs.create(a11folder);
		System.out.println("result afolderResult=" + a11folderResult);
		
		Files filePic3 = new Files();
		filePic3.setFileName("3.jpg");
		filePic3.setParentFolder("/");
		filePic3.setFileType("image");
		filePic3.setCreateTime(new Date());
		filePic3.setUser(user);
		int filePic3Result = fs.create(filePic3);
		System.out.println("result afolderResult=" + filePic3Result);
		
		Files filePic4 = new Files();
		filePic4.setFileName("4.jpg");
		filePic4.setParentFolder("/");
		filePic4.setFileType("image");
		filePic4.setCreateTime(new Date());
		filePic4.setUser(user);
		int filePic4Result = fs.create(filePic4);
		System.out.println("result afolderResult=" + filePic4Result);
		
		Files filePic = new Files();
		filePic.setFileName("a1.jpg");
		filePic.setParentFolder("/a");
		filePic.setFileType("image");
		filePic.setCreateTime(new Date());
		filePic.setUser(user);
		int filePicResult = fs.create(filePic);
		System.out.println("result afolderResult=" + filePicResult);
		
		Files filePic2 = new Files();
		filePic2.setFileName("a2.jpg");
		filePic2.setParentFolder("/a");
		filePic2.setFileType("image");
		filePic2.setCreateTime(new Date());
		filePic2.setUser(user);
		int filePic2Result = fs.create(filePic2);
		System.out.println("result afolderResult=" + filePic2Result);
		
//		User user = new User();
//		user.setUsername("zhang");
//		Files fold = new Files();
//		fold.setFileName("/a/3.jpg");
//		fold.setFileType("image");
//		fold.setCreateTime(new Date());
//		fold.setDirectory(false);
//		fold.setUser(user);
//		result = fs.create(fold);
		
//		result = fs.rename("/e", "f");

//		User user = new User();
//		user.setUsername("zhang");
//		Files file1 = new Files();
//		file1.setFileName("/a/b/3.jpg");
//		file1.setFileType("image");
//		file1.setCreateTime(new Date());
//		file1.setUser(user);
//		
//		Files file2 = new Files();
//		file2.setFileName("/a/e/3.jpg");
//		file2.setFileType("image");
//		file2.setCreateTime(new Date());
//		file2.setUser(user);
//		
//		Files[] filess = new Files[5];
//		filess[0] = file1;
//		filess[1] = file2;
//		
//		fs.move(filess, "/a/f");
		
//		User user = new User();
//		user.setUsername("zhang");
//		Files file1 = new Files();
//		file1.setFileName("/a/1.jpg");
//		file1.setFileType("image");
//		file1.setCreateTime(new Date());
//		file1.setUser(user);
//		
//		Files file2 = new Files();
//		file2.setFileName("/a/aa");
//		file2.setFileType("image");
//		file2.setCreateTime(new Date());
//		file2.setUser(user);
//		
//		Files[] filess = new Files[5];
//		filess[0] = file1;
//		filess[1] = file2;
//		result = fs.del(filess);
		
		//get file list
		FmListConditions fmlc = new FmListConditions();
		fmlc.setDesc(0);
		fmlc.setParentFolder("/");
		fmlc.setOrder("fileName");
//		fmlc.setStartDate(DateUtil.stringToDate("2016-1-1"));
//		fmlc.setEndDate(DateUtil.stringToDate("2016-3-16"));
		List<Files> list = fs.getList(fmlc);
		System.out.println("list=" + JSON.toJSONString(list));

		System.out.println("result=" + result);
		System.out.println("end");
	}

}
